package org.hsqldb;

import org.hsqldb.util.DatabaseManager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * Created by Zhen on 2015/4/18.
 */
public class HsqldbUtils implements HsqldbConstant {
    public static void main(String[] args) {
        DatabaseManager.main(new String[]{
                "-driver", DRIVER,
                "-url", URL,
                "-user", USER,
                "-password", PASSWORD,
        });
    }

    public static void start(String databaseRoot) {
        if(!databaseRoot.endsWith("\\") && !databaseRoot.endsWith("/")) {
            databaseRoot = databaseRoot + "\\";
        }
        System.out.println(databaseRoot);
        Server.main(new String[]{
                "-database.0", databaseRoot + ALIAS + ";user=sa;password=" + PASSWORD,
                "-dbname.0", ALIAS,
                "-no_system_exit", "true"
        });

        /*
        // 设置用户sa的密码
        try {
            Class.forName("org.hsqldb.jdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/" + ALIAS, "sa", "");
            Statement statement = conn.createStatement();
            statement.executeUpdate("SET PASSWORD '" + PASSWORD + "'");
            statement.close();
            conn.close();
        } catch (Exception e) {
            //如果连不上说明密码已设，不用管了。
        }*/
    }

    public static void shutdown() {
        try {
            Class.forName(DRIVER);
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            Statement statement = conn.createStatement();
            statement.executeUpdate("SHUTDOWN;");
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}